package com.eascs.admin.o2o.loan.notify.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.admin.o2o.badloan.entity.O2oCacleEntity;
import com.eascs.admin.o2o.badloan.service.O2oCancleServiceImpl;
import com.eascs.admin.o2o.cacle.entity.O2oCacleFlowEntity;
import com.eascs.admin.o2o.cacle.service.O2oCacleFlowServiceImpl;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.notify.O2OMsgNotifyService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.workflow.common.entity.wf.ProcessTaskMain;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.common.vo.WFNotifyParam;

/**
 * 作废流程
 */
@Service
public class CacleMsgNotifyServiceImpl implements O2OMsgNotifyService {
	private static Logger log = LoggerFactory.getLogger(CacleMsgNotifyServiceImpl.class);
	@Autowired
	private O2oCacleFlowServiceImpl o2oCacleFlowServiceImpl;
	@Autowired
	private O2oCancleServiceImpl o2oCancleServiceImpl;
	
	@Override
	public ModelResult<O2oLoanapplyEntity> notify(WFNotifyParam notifyParam, O2oLoanapplyEntity loanapply){
		ModelResult<O2oLoanapplyEntity> modelResult =new ModelResult<>();
		log.info("作废流程通知:[{}]",StatusContexts.WFTYPE_CACLE);
		loanapply = cacleFlow(notifyParam,loanapply);
		modelResult.withModel(loanapply);
		return modelResult;
	}

	@Override
	public List<String> getServiceName() {
		ArrayList<String> list = new ArrayList<String>();
		list.add("PROCESS_CACLE");
		return list;
	}

	@Override
	public Boolean checkReturn() {
		return Boolean.TRUE;
	}
	
	/**
	 * 征信流程
	 * 
	 * @param notifyParam
	 * @param loanapplyEntity
	 * @return
	 */
	private O2oLoanapplyEntity cacleFlow(WFNotifyParam notifyParam, O2oLoanapplyEntity loanapplyEntity) {
		
		O2oCacleFlowEntity o2oCacleFlowEntity = o2oCacleFlowServiceImpl.getEntityById(notifyParam.getReferId());
		if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_PASS) {// 流程通过
			log.info("贷款编号:[{}],手动作废流程完成,修改为状态为：[{}]", o2oCacleFlowEntity.getBillCode(), StatusContexts.WFTYPE_STATUS_CLO);
			o2oCacleFlowEntity.setCcStatus(StatusContexts.WFTYPE_STATUS_CLO);
			//调用cxw之前的作废方法
			O2oCacleEntity cacleEntity = new O2oCacleEntity();
			cacleEntity.settType(StatusContexts.CANCLE_TYPE);
			cacleEntity.settRemark(o2oCacleFlowEntity.getCacleMessage());
			cacleEntity.setLoanapplyNo(o2oCacleFlowEntity.getLoanapplyNo());
			cacleEntity.settOpername(o2oCacleFlowEntity.getCreateName());
			cacleEntity.settOperno(o2oCacleFlowEntity.getCreateId());
			cacleEntity.settTime(new Date());
			cacleEntity.settIsSuccess(StatusContexts.O2O_YES);
			cacleEntity.settIsSuccess(StatusContexts.O2O_NO);
			cacleEntity.settSysType(StatusContexts.CANCLE_TYPE_SG);
			log.info("作废记录对象：{}", cacleEntity);
			int j = o2oCancleServiceImpl.insertCalcle(cacleEntity);
			if (j <= 0) {
				log.info("手工作废单号：[{}],记录不成功，不能作废，请重新操作。", cacleEntity.getLoanapplyNo());
			}
			com.eascs.admin.common.vo.ModelResult<Boolean> r = o2oCancleServiceImpl.worlFlowCancle(cacleEntity);
			if (r.isSuccess()) {
				log.info("手工作废单号：[{}],记录成功，不能作废，请重新操作。", cacleEntity.getLoanapplyNo());
			} else {
				log.info("作废单号：[{}]失败, message:[{}]", cacleEntity.getLoanapplyNo(), r.getErrMsg());
			}
			
		} else if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_TURNED_DOWN) {// 流程驳回
			log.info("贷款编号:[{}],手动作废流程驳回,修改状态为：[{}]", loanapplyEntity.getBillCode(), StatusContexts.O2O_NEW);
			o2oCacleFlowEntity.setCcStatus(StatusContexts.WFTYPE_STATUS_NEW);
		} else if (notifyParam.getProcessStatus().equals(ProcessTaskMain.PROECESS_STATUS_REJECT)) {// 作废
			log.info("贷款编号:[{}],手动作废流程作废,修改状态为：[{}]", loanapplyEntity.getBillCode(), StatusContexts.O2O_FK_OVER);
			o2oCacleFlowEntity.setCcStatus(StatusContexts.WFTYPE_STATUS_NEW);
		}
		o2oCacleFlowEntity.setEndDate(new Date());
		o2oCacleFlowServiceImpl.update(o2oCacleFlowEntity);
		return loanapplyEntity;
	}
	
}
